// 玩家行为日志记录

package plog

import (
	"GameServer/Common/utils"
	"GameServer/Game/dbmodule"
	"path"

	"GameServer/Game/m"

	"github.com/globalsign/mgo/bson"
	rotatelogs "github.com/lestrrat/go-file-rotatelogs"
	"github.com/rifflock/lfshook"
	log "github.com/sirupsen/logrus"
)

var (
	logWabao    = log.New()
	logSlot     = log.New()
	logShopBuy  = log.New()
	logHideShop = log.New()
)

func InitPlog(logPath string) (err error) {
	writerWabao, err := rotatelogs.New(
		path.Join(logPath, "wabao")+".%Y%m%d%H%M",
		rotatelogs.WithLinkName(path.Join(logPath, "wabao")), // 生成软链，指向最新日志文件
	)
	if err != nil {
		return
	}

	lfHookWabao := lfshook.NewHook(lfshook.WriterMap{
		log.DebugLevel: writerWabao, // 为不同级别设置不同的输出目的
		log.InfoLevel:  writerWabao,
		log.WarnLevel:  writerWabao,
		log.ErrorLevel: writerWabao,
		log.FatalLevel: writerWabao,
		log.PanicLevel: writerWabao,
	}, &log.JSONFormatter{})

	logWabao.AddHook(lfHookWabao)
	logWabao.SetLevel(log.DebugLevel)

	writerSlot, err := rotatelogs.New(
		path.Join(logPath, "slot")+".%Y%m%d%H%M",
		rotatelogs.WithLinkName(path.Join(logPath, "slot")), // 生成软链，指向最新日志文件
	)
	if err != nil {
		return
	}

	lfHookSlot := lfshook.NewHook(lfshook.WriterMap{
		log.DebugLevel: writerSlot, // 为不同级别设置不同的输出目的
		log.InfoLevel:  writerSlot,
		log.WarnLevel:  writerSlot,
		log.ErrorLevel: writerSlot,
		log.FatalLevel: writerSlot,
		log.PanicLevel: writerSlot,
	}, &log.JSONFormatter{})

	logSlot.AddHook(lfHookSlot)
	logSlot.SetLevel(log.DebugLevel)

	writerShopBuy, err := rotatelogs.New(
		path.Join(logPath, "shop")+".%Y%m%d%H%M",
		rotatelogs.WithLinkName(path.Join(logPath, "shop")), // 生成软链，指向最新日志文件
	)
	if err != nil {
		return
	}

	lfHookShopBuy := lfshook.NewHook(lfshook.WriterMap{
		log.DebugLevel: writerShopBuy, // 为不同级别设置不同的输出目的
		log.InfoLevel:  writerShopBuy,
		log.WarnLevel:  writerShopBuy,
		log.ErrorLevel: writerShopBuy,
		log.FatalLevel: writerShopBuy,
		log.PanicLevel: writerShopBuy,
	}, &log.JSONFormatter{})

	logShopBuy.AddHook(lfHookShopBuy)
	logShopBuy.SetLevel(log.DebugLevel)

	writerHideShopBuy, err := rotatelogs.New(
		path.Join(logPath, "shop")+".%Y%m%d%H%M",
		rotatelogs.WithLinkName(path.Join(logPath, "shop")), // 生成软链，指向最新日志文件
	)
	if err != nil {
		return
	}

	lfHookHideShopBuy := lfshook.NewHook(lfshook.WriterMap{
		log.DebugLevel: writerHideShopBuy, // 为不同级别设置不同的输出目的
		log.InfoLevel:  writerHideShopBuy,
		log.WarnLevel:  writerHideShopBuy,
		log.ErrorLevel: writerHideShopBuy,
		log.FatalLevel: writerHideShopBuy,
		log.PanicLevel: writerHideShopBuy,
	}, &log.JSONFormatter{})

	logShopBuy.AddHook(lfHookHideShopBuy)
	logShopBuy.SetLevel(log.DebugLevel)
	return
}

// 挖宝日志
type WaBaoLog struct {
	Id       bson.ObjectId `bson:"_id" json:"id"`
	Uid      string        `bson:"uid" json:"uid"`
	Name     string        `bson:"name" json:"name"`
	Time     int64         `bson:"time" json:"time"`
	Res      []*m.Goods    `bson:"res" json:"res"`
	GoodsTid int64         `bson:"goodsTid" json:"goodsTid"`
	UProp    *m.UpdateProp `bson:"uProp" json:"uProp"`
}

// 老虎机日志
type SlotLog struct {
	Id    bson.ObjectId `bson:"_id" json:"id"`
	Uid   string        `bson:"uid" json:"uid"`
	Name  string        `bson:"name" json:"name"`
	Time  int64         `bson:"time" json:"time"`
	Key   int64         `bson:"key" json:"key"`
	Res   []*m.Goods    `bson:"res" json:"res"`
	UProp *m.UpdateProp `bson:"uProp" json:"uProp"`
	Spin  int64         `bson:"spin" json:"spin"`
}

// 商店日志
type ShopBuyLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	Time   int64         `bson:"time" json:"time"`
	Item   *m.ShopItem   `bson:"item" json:"item"`
	Res    []*m.Goods    `bson:"res" json:"res"`
	FRes   []*m.Goods    `bson:"fres" json:"fres"`
	BoxRes []*m.BoxRes   `bson:"boxRes" json:"boxRes"`
	UProp  *m.UpdateProp `bson:"uProp" json:"uProp"`
}

type HideShopLog struct {
	Id    bson.ObjectId `bson:"_id" json:"id"`
	Uid   string        `bson:"uid" json:"uid"`
	Name  string        `bson:"name" json:"name"`
	Time  int64         `bson:"time" json:"time"`
	Gid   int64         `bson:"gid" json:"gid"`
	Res   []*m.Goods    `bson:"res" json:"res"`
	UProp *m.UpdateProp `bson:"uProp" json:"uProp"`
}

// 被偷日志
type BeTheftLog struct {
	Id      bson.ObjectId `bson:"_id" json:"id"`
	Uid     string        `bson:"uid" json:"uid"`
	Name    string        `bson:"name" json:"name"`
	FromUid string        `bson:"fromUid" json:"fromUid"`
	Time    int64         `bson:"time" json:"time"`
	Gold    int64         `bson:"gold" time:"gold"`
}

// 偷盗日志
type TheftLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	TUid   string        `bson:"tuid" json:"tuid"`
	TName  string        `bson:"tname" json:"tname"`
	TRobot bool          `bson:"tRobot" json:"tRobot"`
	Time   int64         `bson:"time" json:"time"`
	Num    int64         `bson:"num" json:"num"`
	Goods  *m.Goods      `bson:"goods" time:"goods"`
	Res    []*m.Goods    `bson:"res" json:"res"`
	UProp  *m.UpdateProp `bson:"uProp" json:"uProp"`
}

// 被放火日志
type BeFireLog struct {
	Id      bson.ObjectId `bson:"_id" json:"id"`
	Uid     string        `bson:"uid" json:"uid"`
	Name    string        `bson:"name" json:"name"`
	FromUid string        `bson:"fromUid" json:"fromUid"`
	Time    int64         `bson:"time" json:"time"`
	Pos     *m.PosInfo    `bson:"pos" json:"pos"`
	Gids    []int64       `bson:"gids" json:"gids"`
}

// 放火日志
type FireLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	TUid   string        `bson:"tuid" json:"tuid"`
	TName  string        `bson:"tname" json:"tname"`
	TRobot bool          `bson:"tRobot" json:"tRobot"`
	Time   int64         `bson:"time" json:"time"`
	Res    []*m.Goods    `bson:"res" json:"res"`
	UProp  *m.UpdateProp `bson:"uProp" json:"uProp"`
}

// 金币日志
type GoldLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	From   string        `bson:"from" json:"from"`
	OldVal int64         `bson:"oldVal" json:"oldVal"`
	NewVal int64         `bson:"newVal" json:"newVal"`
	Val    int64         `bson:"val" json:"val"`
	Time   int64         `bson:"time" json:"time"`
}

// 砖石日志
type DiamonLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	From   string        `bson:"from" json:"from"`
	OldVal int64         `bson:"oldVal" json:"oldVal"`
	NewVal int64         `bson:"newVal" json:"newVal"`
	Val    int64         `bson:"val" json:"val"`
	Time   int64         `bson:"time" json:"time"`
}

// 体力日志
type EnergyLog struct {
	Id     bson.ObjectId `bson:"_id" json:"id"`
	Uid    string        `bson:"uid" json:"uid"`
	Name   string        `bson:"name" json:"name"`
	From   string        `bson:"from" json:"from"`
	OldVal int64         `bson:"oldVal" json:"oldVal"`
	NewVal int64         `bson:"newVal" json:"newVal"`
	Val    int64         `bson:"val" json:"val"`
	Time   int64         `bson:"time" json:"time"`
}

// 用户日常日志
type UserDailyLog struct {
	Id        bson.ObjectId `bson:"_id" json:"id"`
	Uid       string        `bson:"uid" json:"uid"`             //用户uid
	Name      string        `bson:"name" json:"name"`           //用户名字
	LoginTime int64         `bson:"loginTime" json:"loginTime"` //用户登录时间
	Time      int64         `bson:"time" json:"time"`
}

// 用户登录地址
type UserAddrLog struct {
	Id           bson.ObjectId `bson:"_id" json:"id"`
	Uid          string        `bson:"uid" json:"uid"`   //用户uid
	Name         string        `bson:"name" json:"name"` //用户名字
	RegisterTime int64         `bson:"registerTime" json:"registerTime"`
	Addr         string        `bson:"addr" json:"addr"` //用户登录地址
	Time         int64         `bson:"time" json:"time"`
}

// 挖宝日志
func Log_WaBaoLog(u *m.User, goodsTid int64, res []*m.Goods, uProp *m.UpdateProp) {
	data := &WaBaoLog{
		Id:       bson.NewObjectId(),
		Uid:      u.Uid.Hex(),
		Name:     u.Name,
		Time:     utils.TNow().Unix(),
		Res:      res,
		GoodsTid: goodsTid,
		UProp:    uProp,
	}
	dbmodule.MgoDB.GameLogDB.Insert("WaBaoLog", data)
}

// 放火日志
func Log_FireLog(u *m.User, tu *m.User, tRobot bool, res []*m.Goods, uProp *m.UpdateProp) {
	data := &FireLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		TUid:   tu.Uid.Hex(),
		TName:  tu.Name,
		TRobot: tRobot,
		Time:   utils.TNow().Unix(),
		Res:    res,
		UProp:  uProp,
	}
	dbmodule.MgoDB.GameLogDB.Insert("FireLog", data)
}

// 被放火日志
func Log_BeFireLog(u *m.User, fromUid string, pos *m.PosInfo, gids []int64) {
	data := &BeFireLog{
		Id:      bson.NewObjectId(),
		Uid:     u.Uid.Hex(),
		Name:    u.Name,
		FromUid: fromUid,
		Time:    utils.TNow().Unix(),
		Pos:     pos,
		Gids:    gids,
	}
	dbmodule.MgoDB.GameLogDB.Insert("BeFireLog", data)
}

// 老虎机日志
func Log_SlotLog(u *m.User, key int64, res []*m.Goods, uProp *m.UpdateProp, spin int64) {
	data := &SlotLog{
		Id:    bson.NewObjectId(),
		Uid:   u.Uid.Hex(),
		Name:  u.Name,
		Time:  utils.TNow().Unix(),
		Key:   key,
		Res:   res,
		UProp: uProp,
		Spin:  spin,
	}
	dbmodule.MgoDB.GameLogDB.Insert("SlotLog", data)
}

// 商城购买信息日志
func Log_ShopBuy(u *m.User, item *m.ShopItem, res []*m.Goods, fres []*m.Goods, boxRes []*m.BoxRes, uProp *m.UpdateProp) {
	data := &ShopBuyLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		Time:   utils.TNow().Unix(),
		Item:   item,
		Res:    res,
		FRes:   fres,
		BoxRes: boxRes,
		UProp:  uProp,
	}
	dbmodule.MgoDB.GameLogDB.Insert("ShopBuyLog", data)
}

//  神秘商店购买日志
func Log_HideShop(u *m.User, gid int64, res []*m.Goods, uProp *m.UpdateProp) {
	data := &HideShopLog{
		Id:    bson.NewObjectId(),
		Uid:   u.Uid.Hex(),
		Name:  u.Name,
		Gid:   gid,
		Res:   res,
		UProp: uProp,
		Time:  utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("HideShopLog", data)
}

// 被偷日志
func Log_BeTheft(u *m.User, fuid string, gold int64) {
	data := &BeTheftLog{
		Id:      bson.NewObjectId(),
		Uid:     u.Uid.Hex(),
		Name:    u.Name,
		FromUid: fuid,
		Time:    utils.TNow().Unix(),
		Gold:    gold,
	}
	dbmodule.MgoDB.GameLogDB.Insert("BeTheftLog", data)
}

// 偷盗日志
func Log_Theft(u *m.User, tu *m.User, num int64, goods *m.Goods, res []*m.Goods, uProp *m.UpdateProp) {
	data := &TheftLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		TUid:   tu.Uid.Hex(),
		TName:  tu.Name,
		TRobot: tu.Robot,
		Time:   utils.TNow().Unix(),
		Num:    num,
		Goods:  goods,
		Res:    res,
		UProp:  uProp,
	}
	dbmodule.MgoDB.GameLogDB.Insert("TheftLog", data)
}

// 用户日常日志
func Log_UserDailyLog(u *m.User) {
	data := &UserDailyLog{
		Id:        bson.NewObjectId(),
		Uid:       u.Uid.Hex(),
		Name:      u.Name,
		LoginTime: u.LastLoginTime,
		Time:      utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("UserDailyLog", data)
}

// 金币日志
func Log_Gold(u *m.User, from string, oldVal, newVal, val int64) {
	data := &GoldLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		From:   from,
		OldVal: oldVal,
		NewVal: newVal,
		Val:    val,
		Time:   utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("GoldLog", data)
}

// 钻石日志
func Log_Diamon(u *m.User, from string, oldVal, newVal, val int64) {
	data := &DiamonLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		From:   from,
		OldVal: oldVal,
		NewVal: newVal,
		Val:    val,
		Time:   utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("DiamonLog", data)
}

// 体力日志
func Log_Energy(u *m.User, from string, oldVal, newVal, val int64) {
	data := &EnergyLog{
		Id:     bson.NewObjectId(),
		Uid:    u.Uid.Hex(),
		Name:   u.Name,
		From:   from,
		OldVal: oldVal,
		NewVal: newVal,
		Val:    val,
		Time:   utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("EnergyLog", data)
}

// 用户登录地址
func Log_UserAddrLog(u *m.User, addr string) {
	data := &UserAddrLog{
		Id:           bson.NewObjectId(),
		Uid:          u.Uid.Hex(),
		Name:         u.Name,
		RegisterTime: u.RegisterTime,
		Addr:         addr,
		Time:         utils.TNow().Unix(),
	}
	dbmodule.MgoDB.GameLogDB.Insert("UserAddrLog", data)
}

// 偷盗日志
func Log_FindAll(tableName string, query, selector, result interface{}) {

	dbmodule.MgoDB.GameLogDB.FindAll(tableName, query, selector, result)
}

// // 保存
// func (logDB *PLogDB) Save(tableName string, docs ...interface{}) {
// 	ms := logDB.MgoSession.Clone()
// 	c := ms.DB(logDB.DbName).C(tableName)
// 	ms.SetMode(mgo.Monotonic, true)

// 	defer ms.Close()
// 	logDB.rw.Lock()
// 	defer logDB.rw.Unlock()
// 	c.Insert(docs...)
// }

// //查询所有数据
// func (logDB *PLogDB) FindAll(tableName string, query, selector, result interface{}) {
// 	ms := logDB.MgoSession.Clone()
// 	c := ms.DB(logDB.DbName).C(tableName)
// 	ms.SetMode(mgo.Monotonic, true)

// 	defer ms.Close()
// 	logDB.rw.Lock()
// 	defer logDB.rw.Unlock()
// 	c.Find(query).Select(selector).All(result)
// }

// 86400*15=129600

// dbmodule.MgoDB.GameDB.shop.createIndex( { "time": 1 }, { expireAfterSeconds: 129600 } )
